package com.xq.jltx.kotlinandroid.mmkv

import android.app.Application
import com.xq.jltx.kotlinandroid.AppApplication
import com.xq.jltx.kotlinandroid.PreferencesDelegate
import java.lang.reflect.Type

/**
 * @author jltxseo
 *         Created by junlintianxia on 2019/12/18.
 */
class SettingsMMKVDelegate<T>(
    key: String?,
    defValue: T,
    type: Type? = null
) :
    MMKVDelegate<T>(key, defValue, type) {
    override val name: String = "settings"
    override val application: Application
        get() = AppApplication.INSTANCE

    companion object {
        @JvmStatic
        val instance: SettingsMMKVDelegate<*> by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            SettingsMMKVDelegate("", "")
        }

        @JvmStatic
        fun clearSP() {
            instance.clearSPCore()
        }

        @JvmStatic
        fun clearSP(key: String?) {
            key?.let {
                instance.clearSPCore(it)
            }
        }

        @JvmStatic
        fun contains(key: String?): Boolean {
            return if (key != null) {
                instance.containsCore(key)
            } else {
                false
            }
        }

        @JvmStatic
        fun getAll(): Map<String, *> {
            return instance.getAllCore()
        }

        @JvmStatic
        @JvmOverloads
        fun <T> getSP(key: String, defValue: T, type: Type? = null): T {
            return instance.getSPCore(key, defValue, type)
        }

        @JvmStatic
        @JvmOverloads
        fun setSP(key: String, value: Any?, isSync: Boolean = false) {
            instance.setSPCore(key, value, isSync)
        }

//        @JvmStatic
//        var instance: SettingsPreferencesDelegate<*>? = null
//            get() {
//                if (field == null) {
//                    field = SettingsPreferencesDelegate("", "")
//                }
//                return field
//            }
//            private set
    }
}